" vim: foldmethod=marker commentstring="%s

" vim settings
"
" basic stuff {{{
    set nocompatible " this is vim not vi
    set incsearch " display match for pattern when typing it
    set showcmd " display an incomplete command 
    set updatetime=200
    set backspace=indent,eol,start
    syntax enable
    syntax on " is this the same as :syntax enable?
    "set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim71,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
    set runtimepath=$HOME/.vim,$HOME/.vim/after,$VIMRUNTIME
    set so=4 " lines of context
    set ruler " show position of cursor
    set grepprg=grep\ -nH\ $*
    set ignorecase
    set smartcase
    " backspace and cursor keys wrap
    set whichwrap+=<,>,h,l
    " silent bell, see help visualbell
    set noerrorbells
    set novisualbell
    set vb t_vb=
    " set hlsearch "highlight search matches
    " display as much of last line as possible, display unprintable chars as hex
    set display=lastline,uhex
    " set textwidth=80 " restrict column width to 80
    filetype on
    filetype indent on
    filetype plugin on
    " some options for : command line
    set wildignore=.svn,CVS,*.o,*.a,*.class,*.mo,*.la,*.so,*.obj,*.swp,*.jpg,*.png,*.xpm,*.gif,*.pyc,*.class
    set wildmenu
    set wildmode=longest:full,full
    set cmdheight=2
    " for debugging
    " set verbose=10
    " set verbose=9
    set nofsync
" }}}

" tab settings {{{
    " the all important tab settings
    " set softtabstop=4
    set tabstop=8
    set expandtab
    set shiftwidth=4
    set smarttab autoindent
" }}}

" folding {{{
    " automatically save and restore folds
    " au BufWinLeave * mkview
    " au BufWinEnter * silent loadview
    set foldmethod=marker
    " default fold level, all open, set it 200 or something
    " to make it all closed.
    set foldlevel=0
    set foldcolumn=1 " width of foldcolumn
    " make fillchars empty
    set fillchars=fold:\  

    func! MyFoldText() 
        let line = getline(v:foldstart)
        " delete comment-type chars
        let sub = substitute(line, '/\*\|\*/\|{{{\d\=\|//\|{\|\"\|#', '', 'g')
        " eat up whitespace
        let sub = substitute(sub, '^\s\+\|\s\+$', '', 'g')
        " new text for folds
        let sub = v:folddashes . v:folddashes . '{ ' . sub . ' }'
        return sub
        " fix folds we have introduced
        "}}}
    endfunc
    set foldtext=MyFoldText()

" }}}

" buffer handling {{{
    " when changing buffers, if a buffer becomes abandoned it becomes 'hidden', so
    " it doesn't have to be saved. That is, a buffer may become hidden and with
    " unsaved changes. Probably better that using autowrite
    set hidden
    set confirm " save changes before switching

    " Don't close window, when deleting a buffer
    function! <SID>BufcloseCloseIt()
       let l:currentBufNum = bufnr("%")
       let l:alternateBufNum = bufnr("#")

       if buflisted(l:alternateBufNum)
         buffer #
       else
         bnext
       endif

       if bufnr("%") == l:currentBufNum
         new
       endif

       if buflisted(l:currentBufNum)
         execute("bdelete! ".l:currentBufNum)
       endif
    endfunction
    map <leader>bd :Bclose<cr>
    command! Bclose call <SID>BufcloseCloseIt()
" }}}

" window handling {{{
    " make windows equal in width and height
    " tip: use winfixheight and winfixwidth to make these unaffected
    set equalalways
" }}}

" completion {{{
    " set code completion for each filetype
    autocmd FileType python set omnifunc=pythoncomplete#Complete
    autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
    autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
    autocmd FileType css set omnifunc=csscomplete#CompleteCSS
    autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
    autocmd FileType php set omnifunc=phpcomplete#CompletePHP
    autocmd FileType c set omnifunc=ccomplete#CompleteCpp
    " dictionary for tab completion
    " set dictionary= /usr/share/dict/words

    " for ordinary c-n/c-p completion
    set complete=.,t,i,b,w,k,u
    " put longest common text
    set completeopt=menu
    " set completeopt+=longest
    " make behavior of enter, esc, up, down more familiar 
    inoremap <expr> <Esc>      pumvisible()?"\<C-E>":"\<Esc>"
    inoremap <expr> <CR>       pumvisible()?"\<C-Y>":"\<CR>"
    inoremap <expr> <Down>     pumvisible()?"\<C-N>":"\<Down>"
    inoremap <expr> <Up>       pumvisible()?"\<C-P>":"\<Up>" 
" }}}

" appearance and colors {{{
        set background=dark
    " colorschemes I like
    " golden            **
    " hhdblue           ***
    " hhdgray           **
    " hhdgreen          **
    " neverness         ***
    " oceanblack        ****
    " relaxedgreen      ****
    " wintersday        ***
    " bluegreen         *
    " darkblue          *
    " desertedocean     *****
    " dusk              ****
    " hhazure           ****
    " nightshimmer      **
    " northsky          ****
    " oceandeep         *****
    " softblue          *
    " candycode         ***
    " candy             ***
    " lettuce           **
    " rdark             ****
    " camo              **
    " desert            ***
    " desertEx          ***
    " freya             ***
    " zenburn           **
    " darkocean         *****

    if has("gui_running")
        if hostname() == 'dimatura-laptop'
            set guifont=Bitstream\ Vera\ Sans\ Mono\ 6
        else
            set guifont=Bitstream\ Vera\ Sans\ Mono\ 8
        endif
        " note: m is menu bar flag, L is left hand scrollbar when split,
        " T is toolbar
        " r is right scrollbar
        set guioptions-=m
        set guioptions-=T
        set guioptions-=r
        set guioptions-=L
        set guiheadroom=0
        colorscheme darkocean
        " set cursorline
        hi CursorLine guibg=#333333
        hi CursorColumn guibg=#555555 guifg=#ffffff
    else 
        " this one looks decent in terminals
        colorscheme dante
        " popup menu
        highlight   Pmenu               term=NONE cterm=NONE ctermfg=7 ctermbg=5 gui=NONE guifg=White guibg=Magenta
        highlight   PmenuSel            term=NONE cterm=NONE ctermfg=0 ctermbg=7 gui=NONE guifg=Black guibg=White
        highlight   PmenuSbar           term=NONE cterm=NONE ctermfg=7 ctermbg=0 gui=NONE guifg=White guibg=Black
        highlight   PmenuThumb          term=NONE cterm=NONE ctermfg=0 ctermbg=7 gui=NONE guifg=Black guibg=White

        " highlight clear FoldColumn
        highlight FoldColumn ctermbg=Black ctermfg=Gray
    endif

    autocmd CursorMovedI * if pumvisible() == 0|pclose|endif
    autocmd InsertLeave * if pumvisible() == 0|pclose|endif

    " has to be after colorscheme. maybe should use the autocmd Colorscheme
    " autocmd ColorScheme highlight clear Folded
    " autocmd ColorScheme highlight Folded term=bold gui=bold guifg=#22a865 
    highlight clear Folded 
    highlight Folded term=bold gui=bold guifg=#22a865 

" }}}

" mappings {{{

    " buffers  {{{ 
        map <C-N> <ESC>:bn<CR>
        map <C-P> <ESC>:bp<CR>
        "Fast open a buffer by search for a name
        " map <c-q> :sb

        " open buffer explorer plugin
        noremap <F3> <ESC>:BufExplorer<CR>
        " noremap <F3> :TSelectBuffer<CR>

    " }}}

    " window movement {{{
        " problem: c-j also used by imap plugin
        " map! <C-J> <C-W>j
        " nmap <C-K> <C-W>k
        " nmap <C-H> <C-W>h
        " nmap <C-L> <C-W>l
        " nmap <Down> <C-W>j
        " nmap <Up> <C-W>k
        " nmap <Left> <C-W>h
        " nmap <Right> <C-W>l
    " }}}

    " other {{{

        " reflow paragraph text
        nnoremap Q gqap 
        " reflow visually selected lines
        vnoremap Q gq
        nnoremap <silent> <F2> :MRU<CR>
        " noremap <silent> <F2> :TRecentlyUsedFiles<CR>
        " set pwd to cwd of current file
        map <leader>cd :cd %:p:h<cr>
        " toggle taglist buffer
        nnoremap <silent> <F8> :TlistToggle<CR>
        map <leader>t :Tlist<cr>

        nnoremap <F12> :!ctags-exuberant -R *<CR>
    " }}}

" }}}

" abbreviations {{{
    " command line configs
    cnoreabbrev Wq wq
    cnoreabbrev W w

" }}}

" plugin settings {{{

    " don't load matchit, i haven't used it much
    :let loaded_matchit = 1

    " TagList settings {{{
        let Tlist_WinWidth=50  
        let Tlist_Inc_Winwidth=0
        let Tlist_Enable_Fold_Column=0
        let Tlist_Process_File_Always=1
        let Tlist_Sort_Type="name"
        let Tlist_Display_Prototype=1
        let Tlist_Display_Tag_Scope=0
        " let Tlist_File_Fold_Auto_Close=1
        highlight TagListFileName guifg=black guibg=lightblue
    " }}}

    " minibufexplorer settings {{{
        " disable mbe
        let loaded_minibufexplorer = 1
        " map ctrl+[hjkl] to window movement commands
        "let g:miniBufExplMapWindowNavVim = 1
        " map ctrl+[arrows] to window movement commands
        "let g:miniBufExplMapWindowNavArrows = 1
        " map <c-tab> and <c-s-tab> to buffer movement
        "let g:miniBufExplMapCTabSwitchBufs = 1
        " make MBE play nicely with taglist
        let g:miniBufExplModSelTarget = 1 
        " mbe size
        let g:miniBufExplMinSize = 1
        let g:miniBufExplMaxSize = 1
        " mbe appears when there is on or more
        "let g:miniBufExplorerMoreThanOne=0
        " fix buf that causes windows to appear without h/l
        let g:miniBufExplForceSyntaxEnable = 1
    " }}}

    " omnicpp settings {{{
        " let OmniCpp_NamespaceSearch = 2
        " let OmniCpp_ShowPrototypeInAbbr = 1
        " let OmniCpp_ShowAccess = 0
        " let OmniCpp_DefaultNamespaces = ["std"]
        " to disable automatic completing for omnicpp
        " let OmniCpp_MayCompleteDot = 0
    " }}}

    " pydoc settings {{{
        " normal pydoc2.5 isn't working... use pydoc2.4 instead
        let g:pydoc_cmd = "pydoc2.4"
    " }}}

    " MRU settings {{{
        let MRU_Max_Entries = 48
        let MRU_Window_Height = 48
    " }}}

    " latex suite settings {{{
        let g:Tex_DefaultTargetFormat="pdf"
        let g:Tex_ViewRule_pdf='xpdf'
    " }}}
    
    " snippetsEmu settings {{{
        " let g:snippetsEmu_key="<C-Q>"
    " }}}

" }}}

" ctags stuff {{{
    " the systags file was generated by: ctags /usr/include/*
    " set tags+=~/.vim/systags
    " set tags=./tags, OTHER_TAG_FILEs " use this one when I have something useful
    " in OTHER_TAG_FILES...
    " set tags+=~/.vim/tagfiles/fltktags
    " set tags+=~/.vim/tagfiles/cursestags
    " set tags+=~/.vim/tagfiles/stdtags
    " set tags+=~/.vim/tagfiles/gltags
    " set tags+=~/.vim/tagfiles/gtk2tags
    " set tags+=~/.vim/tagfiles/gtkmmtags
    " set tags+=~/.vim/tagfiles/wxtags
    " set tags+=$HOME ~/.vim/tagfiles/pythontags
" }}}

" C/C++ settings {{{
"   " cpp snippets {{{
        autocmd FileType cpp inorea <buffer> cfun <c-r>=IMAP_PutTextWithMovement("<++> <++>(<++>) {\n<++>;\nreturn <++>;\n}")<cr>
        autocmd FileType cpp inorea <buffer> cfor <c-r>=IMAP_PutTextWithMovement("for(<++>; <++>; <++>) {\n<++>;\n}")<cr>
        autocmd FileType cpp inorea <buffer> cif <c-r>=IMAP_PutTextWithMovement("if(<++>) {\n<++>;\n}")<cr>
        autocmd FileType cpp inorea <buffer> cifelse <c-r>=IMAP_PutTextWithMovement("if(<++>) {\n<++>;\n}\nelse {\n<++>;\n}")<cr>
        autocmd FileType cpp inorea <buffer> cclass <c-r>=IMAP_PutTextWithMovement("class <++> {\n<++>\n}")<cr>
        autocmd FileType cpp inorea <buffer> cmain <c-r>=IMAP_PutTextWithMovement("int main(int argc, char** argv) {\n<++>\n}")<cr>
        autocmd FileType cpp inorea <buffer> cdbg <c-r>=IMAP_PutTextWithMovement("#ifdef DEBUG\n printf(\"<++>\\n\");\n#endif")<cr>
    " }}}

    autocmd BufNewFile,BufRead *.cpp,*.cxx,*.cc,*.c nnoremap <buffer> <F6> :make <CR>
    autocmd BufNewFile,BufRead *.cpp,*.cxx,*.cc,*.c nnoremap <buffer> <F5> :make go <CR>
    " recommended by :help omnicppcomplete
    autocmd BufNewFile,BufRead *.cpp,*.cxx,*.cc,*.c nnoremap <buffer> <F12> :!ctags-exuberant -R --c++-kinds=+p --fields=+iaS --extra=+q *<CR>
    " autocmd BufNewFile,BufRead *.cxx,*.cc,*.c abbr INC #include
    " autocmd FileType c map <buffer> <leader><space> :w<cr>:!gcc %<cr>
    autocmd FileType c map <buffer> <leader>cc :w<cr>:!gcc %<cr>

" }}}

" misc filetype settings {{{
    " auto make executable
    autocmd BufWritePost *.sh !chmod +x %
    
    " a hack to override colorscheme's color settings
    " maybe use autocmd ColorScheme ?

    autocmd BufNewFile,Bufread *.rem so $HOME/.vim/syntax/remind.vim
    autocmd BufRead *.vala set efm=%f:%l.%c-%[%^:]%#:\ %t%[%^:]%#:\ %m
    autocmd BufRead *.vapi set efm=%f:%l.%c-%[%^:]%#:\ %t%[%^:]%#:\ %m
    au BufRead,BufNewFile *.vala            setfiletype vala
    au BufRead,BufNewFile *.vapi            setfiletype vala
    au! Syntax vala source $VIM/vim71/syntax/cs.vim

    " autocmd filetype help 
" }}}

" Python settings {{{
" 
"
   "Python iMaps (mappings for insert mode)
    autocmd FileType python inorea <buffer> cfun <c-r>=IMAP_PutTextWithMovement("def <++>(<++>):\n<++>\nreturn <++>")<cr>
    autocmd FileType python inorea <buffer> cclass <c-r>=IMAP_PutTextWithMovement("class <++>:\n<++>")<cr>
    autocmd FileType python inorea <buffer> cfor <c-r>=IMAP_PutTextWithMovement("for <++> in <++>:\n<++>")<cr>
    autocmd FileType python inorea <buffer> cif <c-r>=IMAP_PutTextWithMovement("if <++>:\n<++>")<cr>
    autocmd FileType python inorea <buffer> cifelse <c-r>=IMAP_PutTextWithMovement("if <++>:\n<++>\nelse:\n<++>")<cr>
    autocmd FileType python inoremap <buffer> $r return
    autocmd FileType python inoremap <buffer> $s self
    autocmd FileType python inoremap <buffer> $c ##<cr>#<space><cr>#<esc>kla
    autocmd FileType python inoremap <buffer> $i import
    autocmd FileType python inoremap <buffer> $p print
    autocmd FileType python inoremap <buffer> $d """<cr>"""<esc>O

    " insert a breakpoint in insert and normal mode
    autocmd FileType python inoremap <buffer> $b pdb.set_trace()
    autocmd FileType python nnoremap <buffer> <f7> opdb.set_trace()<esc>

    func! DeleteTrailingWS()
      exe "normal mz"
      %s/\s\+$//ge
      exe "normal `z"
    endfunc
    autocmd BufWrite *.py :call DeleteTrailingWS()

    " compile / make plugins 
    autocmd FileType python compiler pylint

    " <buffer> means mapping is local
    " only in normal mode, no remap
    autocmd FileType python nnoremap <buffer> <F6> :w<cr>:make<cr>
    autocmd FileType python nnoremap <buffer> <F5> :w<cr>:!python %<CR>

    " syntax coloring for python options
    let python_highlight_numbers = 1
    let python_highlight_builtins = 1
    let python_highlight_exceptions = 1
    let python_highlight_space_errors = 1

    " unused code {{{
    "
    " add a big pydictionary
    " autocmd FileType python set complete+=k$HOME/.vim/pydiction iskeyword+=.,(
    "
    " ability to jump to python class libs using 'gf' on import statements"
    "  python << EOF
    "  import os
    "  import sys
    "  import vim
    "  for p in sys.path:
    "      if os.path.isdir(p):
    "          vim.command(r"set path+=%s" % (p.replace(" ",r"\ ")))
    "  EOF

    " Run in the Python interpreter
    " function! Python_Eval_VSplit() range
    "  let src = tempname()
    "  let dst = tempname()
    "  execute ": " . a:firstline . "," . a:lastline . "w " . src
    "  execute ":!python " . src . " > " . dst
    "  execute ":pedit! " . dst
    " endfunction
    " au FileType python vmap <F7> :call Python_Eval_VSplit()<cr>
    ""}}}

" }}}

" java settings"{{{
   " java snippets {{{
        autocmd FileType java inorea <buffer> cfun <c-r>=IMAP_PutTextWithMovement("public<++> <++>(<++>) {\n<++>;\nreturn <++>;\n}")<cr>
        autocmd FileType java inorea <buffer> cfunpr <c-r>=IMAP_PutTextWithMovement("private<++> <++>(<++>) {\n<++>;\nreturn <++>;\n}")<cr>
        autocmd FileType java inorea <buffer> cfor <c-r>=IMAP_PutTextWithMovement("for(<++>; <++>; <++>) {\n<++>;\n}")<cr>
        autocmd FileType java inorea <buffer> cif <c-r>=IMAP_PutTextWithMovement("if(<++>) {\n<++>;\n}")<cr>
        autocmd FileType java inorea <buffer> cifelse <c-r>=IMAP_PutTextWithMovement("if(<++>) {\n<++>;\n}\nelse {\n<++>;\n}")<cr>
        autocmd FileType java inorea <buffer> cclass <c-r>=IMAP_PutTextWithMovement("class <++> <++> {\n<++>\n}")<cr>
        autocmd FileType java inorea <buffer> cmain <c-r>=IMAP_PutTextWithMovement("public static void main(String[] argv) {\n<++>\n}")<cr>
    " }}}
    "}}}
    
  " txt2tags settings {{{
    autocmd BufNewFile,BufRead *.t2t set ft=txt2tags
" }}}



